{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 模块\n",
    "- 一个模块就是一个包含python代码的文件，后缀名称是.py就可以，模块就是个python文件\n",
    "- 为什么用模块\n",
    "    - 程序太大，编写维护非常不方便，需要拆分\n",
    "    - 模块可以增加代码重复利用的方式\n",
    "    - 当做命名空间使用，避免命名冲突\n",
    "- 如何定义模块\n",
    "    - 模块就是一个普通文件，所以任何代码可以直接书写\n",
    "    - 不过根据模块的规范，最好在模块中编写以下内容：\n",
    "        - 函数（单一功能）\n",
    "        - 类（相似功能的组合，或者类似业务模块）\n",
    "        - 测试代码\n",
    "        \n",
    "- 如何使用模块\n",
    "    - 模块直接导入\n",
    "    - 语法\n",
    "    \n",
    "            import module_name\n",
    "            module_name.function_name\n",
    "            module_name.class_name\n",
    "     - import 模块 as 别名\n",
    "     - from module_name import class_name,func_name\n",
    "     - from moduel_name import *\n",
    "     \n",
    "- if __name__ == '__main__': 的使用\n",
    "    - 可以有效避免模块代码被导入的时候被动执行的问题\n",
    "    - 建议所有程序的入口都以此代码作为入口\n",
    "    \n",
    " # 模块的搜索路径和存储\n",
    " - 系统默认的模块搜索路径：\n",
    "      import sys\n",
    "      sys.path 属性可以获取路径列表\n",
    "- 添加搜索路径\n",
    "    sys.path.append(dir)\n",
    "- 模块的加载顺序\n",
    "    - 搜索内存中已经加载好的模块\n",
    "    - 搜索python的内置模块\n",
    "    - 搜索sys.path 路径\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['',\n",
       " '/Users/chengp',\n",
       " '/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python37.zip',\n",
       " '/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7',\n",
       " '/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload',\n",
       " '/usr/local/lib/python3.7/site-packages',\n",
       " '/usr/local/lib/python3.7/site-packages/IPython/extensions',\n",
       " '/Users/chengp/.ipython']"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import sys\n",
    "sys.path"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 包\n",
    "- 包是一种组织管理代码的方式，包里面存放的是模块\n",
    "    - 用于将模块包含在一起的文件夹就是包\n",
    "- 自定义包的结构\n",
    "\n",
    "            /---包\n",
    "            /---/---__init__.py 包的标志文件\n",
    "            /---/--- 模块1\n",
    "            /---/--- 模块2\n",
    "            /---/--- 子包（子文件夹）\n",
    "            /---/---/--- __init__.py 包的标志文件\n",
    "            /---/---/--- 子包模块1\n",
    "            /---/---/--- 子包模块2\n",
    "- 包的导入操作\n",
    "    - import package_name\n",
    "        - 直接导入一个包，可以使用 __init__.py中的内容\n",
    "        - 使用方式是：\n",
    "            package_name.func_name\n",
    "            package_name.class_name.func_name()\n",
    "        - 此种方式的访问内容是：\n",
    "            \n",
    "    - __all__用法\n",
    "        - 在使用from package import * 的时候，*可以导入的内容\n",
    "        - __init__.py中如果文件为空，或者没有__all__，那么只可以把__init__中的内容导入\n",
    "        - __init__.py中如果设置了__all__的值，那么按照__all__指定的子包或者模块进行导入，如此则不会载入__init__中的内容\n",
    "        - __all__=['module1', 'module2', 'package1'.......]\n",
    "        "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 命名空间\n",
    "- 用于区分不同位置不同功能但相同名称的函数或者变量的一个特定前缀\n",
    "- 作用是防止命名冲突\n",
    "        \n",
    "        setName()\n",
    "        Student.setName()\n",
    "        Dog.setName()\n",
    "- "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
